<?php
/**
 * Description of RuanganRapat
 *
 * @author Rezan Achmad
 */
class RuanganRapat extends Ruangan {
        /*
         * Atribute for Smart Search
         */
        public $time_start;
        public $time_end;
        public $participant;
        
        private $_date;
        
        /**
         * Additional attribute for Advanced Search
         */
        public $floor_number;
        public $building_name;
        public $status;
    
        /**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
                $rules = parent::rules();
                $rules[] = array('time_start, time_end, participant, date, floor_number, building_name, status', 'safe', 'on'=>'search');
                return $rules;
	}
        
        /**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search($date)
	{
		$criteria=new CDbCriteria;
                
                $criteria->with = array('floor', 'floor.building');
                
                $criteria->compare('floor.floor_number', $this->floor_number);
                $criteria->compare('building.name', $this->building_name,true);
		$criteria->compare('t.name',$this->name,true);
		$criteria->compare('capacity',$this->capacity);
                $criteria->addCondition('type_ruangan_id = 1');
                
                $filtersStatus = explode(" ", $this->status);
                $type = $filtersStatus[0];
                $this->time_start = $start = isset($filtersStatus[1]) ? $filtersStatus[1] : '';
                $this->time_end = $end = isset($filtersStatus[2]) ? $filtersStatus[2] : '';
                
                $date = Util::convertDate($this->date);
                
                if($type == "available") {
                    $rapats = Rapat::model()->findAll("date = '$date' AND status!='rejected'");
                    $ignoreId = array();
                    foreach($rapats as $rapat ) {
                        if (!in_array($rapat->room_id, $ignoreId)) {
                            if ($end >= $rapat->time_start && $rapat->time_end >= $start ) {
                                $ignoreId[] = $rapat->room_id;
                            }
                        }
                    }
                    
                    foreach($ignoreId as $i) {
                        $criteria->addCondition("t.id != $i");
                    }
                    
                } else if ($type == "use" || $type == "book") {
                    $rapats = null;
                    if ($type == "use") {
                        $rapats = Rapat::model()->findAll("date = '$date' AND status='approved'");
                    } else {
                        $rapats = Rapat::model()->findAll("date = '$date' AND status='waiting'");
                    }
                    
                    if ($rapats == null) {
                        $criteria->addCondition('FALSE');
                    } else {
                        $first = true;
                        $addId = array();
                        foreach($rapats as $rapat ) {
                            if (!in_array($rapat->room_id, $addId)) {
                                if ($end >= $rapat->time_start && $rapat->time_end >= $start) {
                                    $addId[] = $rapat->room_id;
                                }
                            }
                        }
                        
                        $condition = array();
                        foreach ($addId as $i) {
                            $condition[] = 't.id = ' . $i;
                        }
                        
                        if (empty($condition)) {
                            $criteria->addCondition('FALSE');
                        } else {
                            $criteria->addCondition(implode(' OR ', $condition));
                        }
                    }
                }

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
        
        public function getStatus($date) {
            $date = Util::convertDate($date);
            $rapats = Rapat::model()->findAll('date LIKE "' . $date . '" AND room_id = ' . $this->id);
            $status = "";
            
            foreach($rapats as $r) {
                if ($r->status == "waiting") {
                    $status .= "Booked " . $r->time_start . " - " . $r->time_end;
                } else if ($r->status == "approved") {
                    $status .= "Use " . $r->time_start . " - " . $r->time_end;
                }
                
                $status .= "<br />";
            }
            
            if ($status == "") {
                $status = "Tersedia sepanjang hari";
            }
            
            return $status;
        }
        
        public function getDate() {
            if($this->_date == null) {
                $this->_date = date("d/m/Y", time());
            }
            
            return $this->_date;
        }
        
        public function setDate($value) {
            $timestamp=CDateTimeParser::parse($value,'dd/MM/yyyy');
            if($timestamp != false) {
                $this->_date = $value;
            } else {
                $this->_date = date("d/m/Y", time());
            }
        }
}

?>